/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.core;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Properties;

/**
 *
 * @author neo
 */
public class ListSupportHandler extends ClassSupportHandler {

    @Override
    public String handle(Properties prop, int index, Method method, Class cls) {
        Type type = method.getGenericParameterTypes()[index];
        int p1 = type.toString().indexOf("<");
        int p2 = type.toString().indexOf(">");
        String gClass = type.toString().substring(p1 + 1, p2);
        //System.out.println("GClass[" + gClass + "]");
        StringBuilder sm = new StringBuilder();
        sm.append("\t\tvar buf_").append(index).append(" = new StringBuffer();\n");
        sm.append("\t\tbuf_").append(index).append(".append(\"{javaClass:").append(cls.getName()).append(",\");\n");
        sm.append("\t\tbuf_").append(index).append(".append(\"castingClass:").append(gClass).append(",\");\n");
        sm.append("\t\tbuf_").append(index).append(".append(\"").append(Key.LIST_SIZE).append(":\");\n");
        sm.append("\t\tbuf_").append(index).append(".append(param_").append(index).append(".size());\n");
        sm.append("\t\tbuf_").append(index).append(".append(\",\");\n");
        sm.append("\t\tfor(var i = 0; i < param_").append(index).append(".size(); i++) {\n");
        sm.append("\t\t\tvar obj_").append(index).append(" = param_").append(index).append(".get(i);\n");
        sm.append("\t\t\tbuf_").append(index).append(".append(\"list_\");\n");
        sm.append("\t\t\tbuf_").append(index).append(".append(i);\n");
        sm.append("\t\t\tbuf_").append(index).append(".append(\":\");\n");
        String scase = prop.getProperty(gClass + ".list.support");
        if (scase != null) {
            sm.append("\t\t\tbuf_").append(index).append(".append(\"");
            int icase = Integer.parseInt(scase);
            switch (icase) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                    sm.append("+obj_").append(index).append("+");
                    break;
                case 5:
                case 6:
                    sm.append("\\\"").append("+obj_").append(index).append("+\"\\\"");
                    break;
            }
            sm.append("\"\");\n");
        } else {
            sm.append("\t\t\tbuf_").append(index).append(".append(\"{\");\n");
            sm.append("\t\t\tbuf_").append(index).append(".append(\"javaClass:").append(gClass).append(",\");\n");
            Object obj = null;
            try {
                obj = Class.forName(gClass).newInstance();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            int count = 0;
            Method[] ms = obj.getClass().getDeclaredMethods();
            for (int i = 0; i < ms.length; i++) {
                if (ms[i].getName().startsWith("get")) {
                    count++;
                }
            }
            for (int i = 0, j = 0; i < ms.length; i++) {
                if (ms[i].getName().startsWith("get")) {
                    String prefix = ms[i].getName().substring(3, 4).toLowerCase();
                    String name = prefix + ms[i].getName().substring(4);
                    sm.append("\t\t\tbuf_").append(index).append(".append(\"").append(name).append(":");
                    //System.out.println("Check list support[" + ms[i].getReturnType().getName() + "]");
                    String scase2 = prop.getProperty(ms[i].getReturnType().getName() + ".ptype");
                    int icase = Integer.parseInt(scase2);
                    switch (icase) {
                        case 0:
                            sm.append("");
                            break;
                        case 1:
                            sm.append("\\\"\"").append("+obj_").append(index).append(".").append(name).append("+").append("\"\\\"");
                            break;
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                            sm.append("\"+obj_").append(index).append(".").append(name).append("+\"");
                            break;
                        case 6:
                            sm.append("\\\"\"").append("+obj_").append(index).append(".").append(name).append("+").append("\"\\\"");
                            break;
                    }
                    sm.append("\");\n");
                    if (j < (count - 1)) {
                        sm.append("\t\t\tbuf_").append(index).append(".append(\",\");\n");
                    }
                    j++;
                }

            }
            sm.append("\t\t\tbuf_").append(index).append(".append(\"}\");\n");
        }
        sm.append("\t\t\tif(i < (param_").append(index).append(".size()-1)) {\n");
        sm.append("\t\t\t\t\tbuf_").append(index).append(".append(\",\");\n");
        sm.append("\t\t\t}\n");
        //sm.append("\t\t\tbuf_").append(index).append(".append(\"}\");\n");
        sm.append("\t\t}\n");
        sm.append("\t\t\tbuf_").append(index).append(".append(\"}\");\n");
        return sm.toString();
    }
}
